package cache

import (
	"fmt"
)

// ----------------------------------------
//  Redis 客户端管理器
// ----------------------------------------
type Caches struct {
	rm *RedisClientManager
}

func CreateCaches() *Caches {
	return &Caches{
		rm: CreateRedisClientManager(),
	}
}

func (caches *Caches) RegisterRedisClient(name string, config *RedisConfig) error {
	if client, err := CreateRedisClient(name, config); err != nil {
		return fmt.Errorf("failed to create redis client %s: %s", name, err)
	} else {
		caches.rm.Set(name, client)
		return nil
	}
}

func (caches *Caches) RegisterRedisClients(configs map[string]*RedisConfig) error {
	if len(configs) > 0 {
		for name, config := range configs {
			if err := caches.RegisterRedisClient(name, config); err != nil {
				return err
			}
		}
	}
	return nil
}

func (caches *Caches) Redis(name string) *RedisClient {
	return caches.rm.Get(name)
}
